package com.example;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

public class ListTimeline extends Activity {
	static final String TAG = "ListTimeline";
	private Spinner m_myDynamicSpinner;
	private ListView listTimeline;
	private SharedPreferences prefs;
	private BroadcastReceiver twitterStatusReceiver;
	private BroadcastReceiver twitterListReceiver;
	private Cursor timelineCursor;
	private TimelineAdapter adapter;
	private Cursor listCursor;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_timeline);
		// Find the Spinner Control by id
		m_myDynamicSpinner = (Spinner) findViewById(R.id.spinner);
		m_myDynamicSpinner
				.setOnItemSelectedListener(new MyOnItemSelectedListener());
		listCursor = managedQuery(Timeline.LIST_CONTENT_URI, new String[] {"distinct _id"}, null, null, null);

		SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,
				android.R.layout.simple_spinner_item, listCursor,
				new String[] { "_id" }, new int[] { android.R.id.text1 });
		startManagingCursor(listCursor);
		adapter2
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		m_myDynamicSpinner.setAdapter(adapter2);
		// Find list view by id
		listTimeline = (ListView) findViewById(R.id.listTimeline);

		timelineCursor = managedQuery(Timeline.LIST_TIMELINE_CONTENT_URI, null, null, null, "created_at DESC"); 
			//db.rawQuery("select * from timeline order by created_at DESC;", null);
		startManagingCursor(timelineCursor);

		adapter = new TimelineAdapter(getApplicationContext(), timelineCursor);
		listTimeline.setAdapter(adapter);

		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		prefs
				.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
					public void onSharedPreferenceChanged(
							SharedPreferences arg0, String arg1) {
						new TwitterClient(getApplicationContext())
								.setProxySettings();
					}
				});
		twitterStatusReceiver = new TwitterListStatusReceiver();
		registerReceiver(twitterStatusReceiver, new IntentFilter(
				UpdaterService.ACTION_NEW_TWITTER_STATUS));
		twitterListReceiver = new TwitterListReceiver();
		registerReceiver(twitterListReceiver, new IntentFilter(
				UpdaterService.ACTION_NEW_TWITTER_LIST));

	}

	@Override
	public void onResume() {
		super.onResume();
		// Cancel notification
		NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notificationManager.cancel(UpdaterService.Updater.STATUS_NOTIFICATION_ID);
		notificationManager.cancel(UpdaterService.Updater.LIST_NOTIFICATION_ID);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(twitterStatusReceiver);
		unregisterReceiver(twitterListReceiver);
	}

	class TwitterListStatusReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "onReceive got ACTION_NEW_TWITTER_STATUS broadcast");
			timelineCursor.requery();
		}
	}

	class TwitterListReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "onReceive got ACTION_NEW_TWITTER_LISTS broadcast");
			listCursor.requery();
		}
	}

	public class MyOnItemSelectedListener implements OnItemSelectedListener {

		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			Object spinnerValue = (Object) parent.getSelectedItem();

			String selectedText = (String) ((Cursor) spinnerValue).getString(0);
			// If nothing was selected return
			if (selectedText == "" || selectedText.equals(null))
				return;

			timelineCursor =  managedQuery(Timeline.LIST_TIMELINE_CONTENT_URI, null, "l._id='" +
				selectedText +"'", null, "created_at DESC");  
				
			adapter.changeCursor(timelineCursor);

		}

		public void onNothingSelected(AdapterView<?> parent) {
			// Do nothing.
		}
	}

}
